﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using QuocTeViet.BL;
using QuocTeViet.DO;

namespace QuocTeViet.AdminCP.PageUC
{
    public partial class ucProductBrand : System.Web.UI.UserControl
    {
        cmsNhanHieuDO objNhanHieu = new cmsNhanHieuDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["BID"] != null)
            {
                objNhanHieu.NhanHieuID = int.Parse(Request.QueryString["BID"].ToString());
                InitForm();
            }
        }

        protected void InitForm()
        {
            objNhanHieu = new cmsNhanHieuBL().Select(objNhanHieu);
            txtTenNhanHieu.Text = objNhanHieu.TenNhanHieu.ToString();
            TextBox1.Text = objNhanHieu.eTenNhanHieu;
            txteMoTa.Text = objNhanHieu.eMoTa;
            if (!string.IsNullOrEmpty(Convert.ToString(objNhanHieu.MoTa)))
            {
                txtMoTa.Text = objNhanHieu.MoTa.ToString();
            }
            try
            {
            }
            catch
            {
            }
            if (!string.IsNullOrEmpty(objNhanHieu.Logo))
            {
                hplImage.Visible = true;
                hplImage.NavigateUrl = "~/Media/" + objNhanHieu.Logo;
            }
        }
        
        protected void SetObject()
        {
            objNhanHieu.TenNhanHieu = txtTenNhanHieu.Text;
            objNhanHieu.MoTa = txtMoTa.Text;
            objNhanHieu.eMoTa = txteMoTa.Text;
            objNhanHieu.eTenNhanHieu = TextBox1.Text;
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                string FileName = string.Format("{0}{1}", fulImage.FileName.Substring(0, fulImage.FileName.LastIndexOf(".")) + "-" + DateTime.Now.ToString("dd-MM-yyyy-hhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                objNhanHieu.Logo = FileName;
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {

        }
        long i;
        protected void btnLuu_Click(object sender, EventArgs e)
        {
            SetObject();
            if (objNhanHieu.NhanHieuID <= 0)
            {
                i = new cmsNhanHieuBL().Insert(objNhanHieu);
                Functions.Alert("Thêm mới thành công", "Default.aspx?Page=ListProductBrand");
            }
            else
            {
                i = new cmsNhanHieuBL().Update(objNhanHieu);
            //    i = objNhanHieu.NhanHieuID;
                Functions.Alert("Cập nhật thành công", "Default.aspx?Page=ListProductBrand");
            }
        }
    }
}